<?php
namespace app\api\controller\xykeep;

use app\api\model\xykeep\Goods as GoodsModel;
use app\common\controller\Api;
use app\common\model\Comment as CommentModel;
use think\Db;

class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function home()
    {
        $param = $this->request->post();
        if(isset($param['store_id'])){
            $store_id = $param['store_id'];
        }else{
            $store_id = 0;
        }

        $banner = Db::name('banner')->where('store_id',$store_id)->select();
        $service = Db::name('service')->select();

        $goods_list_ids = GoodsModel::where([
            'store_id' => $store_id,
            'is_recmd' => 1,
        ])->column('id');

        $goods_list = [];
        foreach ($goods_list_ids as $id) {
            $detail = GoodsModel::getDetail($id);
            $goods_list[] = $detail;
        }


        $cates = Db::name('xykeep_category')->where([
            'status' => 'normal',
            'store_id' => $store_id,
        ])->limit(0,7)->order('weigh desc,id desc')->select();


        $this->success('首页', [
            'banner' => $banner,
            'service' => $service,
            'goods_list' => $goods_list,
            'cates' => $cates,
        ]);
    }


    public function store()
    {

        $param = $this->request->post();

        $store_id = 0;
        if(isset($param['store_id'])){
            $store_id = $param['store_id'];
        }else{
            $this->error('参数错误');
        }

        //店铺信息
        $store_info = Db::name('xykeep_store')->where('id',$store_id)->find();

        //广告图
        $banner = Db::name('banner')->where('store_id',$store_id)->select();

        //商品分类
        $cates = Db::name('xykeep_category')->where([
            'status' => 'normal',
            'store_id' => $store_id,
        ])->order('weigh desc,id desc')->select();

        //商品列表
        if(isset($cates[0])){
            $goods_list_ids = Db::name('xykeep_goods')->where([
                'store_id' => $store_id,
                'category_ids' => $cates[0]['id'],
                'status' => 'up',
            ])->order('weigh desc,id desc')->column('id');

            $goods_list = [];
            foreach ($goods_list_ids as $id) {
                $detail = GoodsModel::getDetail($id);
                $goods_list[] = $detail;
            }
        }else{
            $goods_list = [];
        }

        $comment = (new CommentModel)->where([
            'target_id' => $store_id,
        ])->select();


        $this->success('商家首页', [
            'store_info' => $store_info,
            'banner' => $banner,
            'cates' => $cates,
            'goods_list' => $goods_list,
            'comment' => $comment,
        ]);
    }

}